package com.example.leo.mycharmingstudent.a1012;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;

public class TweenAnimationActivity extends AppCompatActivity {

    //透明度动画执行的TextView
    private TextView tvAlpha;
    //旋转动画执行的TextView
    private TextView tvRotate;
    //缩放动画执行的TextView
    private TextView tvScale;
    //平移动画执行的TextView
    private TextView tvTranslate;
    //动画集合执行的TextView
    private TextView tvSet;

    //透明度动画
    private Animation mAlphaAnimation;
    //旋转动画
    private Animation mRotateAnimation;
    //缩放动画
    private Animation mScaleAnimation;
    //平移动画
    private Animation mTranslateAnimation;
    private Animation mAnimationSet;
    private TextView tvInterpolator1;
    private TextView tvInterpolator2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_animation);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        tvAlpha = findViewById(R.id.tv_alpha_activity_tween_animation);
        tvRotate = findViewById(R.id.tv_rotate_activity_tween_animation);
        tvScale = findViewById(R.id.tv_scale_activity_tween_animation);
        tvTranslate = findViewById(R.id.tv_translate_activity_tween_animation);
        tvSet = findViewById(R.id.tv_set_activity_tween_animation);
        tvInterpolator1 = findViewById(R.id.tv_interpolator1_activity_tween_animation);
        tvInterpolator2 = findViewById(R.id.tv_interpolator2_activity_tween_animation);
    }

    private void initData() {
        mAlphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        //动画执行完成以后，显示动画执行完后的样式
        mAlphaAnimation.setFillAfter(true);
        mAlphaAnimation.setRepeatCount(1);

        mRotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        //此处设置repeatCount为何没有生效？
        mRotateAnimation.setRepeatCount(2);

        mScaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
        mTranslateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
        mTranslateAnimation.setFillAfter(true);

        mAnimationSet = AnimationUtils.loadAnimation(this, R.anim.animation_set);
    }

    private void initEvent() {
        tvAlpha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                补间动画流程：
                1.在anim资源文件夹中创建动画xml文件
                2.使用AnimationUtils.loadAnimation方法加载动画
                3.view.startAnimation方法执行动画
                 */
                tvAlpha.startAnimation(mAlphaAnimation);
            }
        });
        tvRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvRotate.startAnimation(mRotateAnimation);
            }
        });
        tvScale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行缩放动画
                tvScale.startAnimation(mScaleAnimation);
            }
        });
        tvTranslate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                补间动画是渲染出来的表象
                表现为View移动到其他地方，点击事件仍在原处才能响应。
                 */
                //执行平移动画
                tvTranslate.startAnimation(mTranslateAnimation);
                ToastUtil.showToast(TweenAnimationActivity.this, "执行了平移动画");
            }
        });

        tvSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //执行动画集合
                tvSet.startAnimation(mAnimationSet);
            }
        });

        tvInterpolator1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveAnimation();
            }
        });

        tvInterpolator2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveAnimation();
            }
        });
    }

    private void moveAnimation() {
        //添加线性差值器
        Animation linearAnimation = AnimationUtils.loadAnimation(
                TweenAnimationActivity.this,
                R.anim.translate);
        linearAnimation.setInterpolator(new LinearInterpolator());
        tvInterpolator1.startAnimation(linearAnimation);

        //添加加速差值器
        Animation accelerateAnimation = AnimationUtils.loadAnimation(
                TweenAnimationActivity.this,
                R.anim.translate
        );
        accelerateAnimation.setInterpolator(new AccelerateInterpolator());
        tvInterpolator2.startAnimation(accelerateAnimation);
    }
}
